﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using InformationSystem.BLL.Core.Model;

namespace InformationSystem.WebSite.Utility
{
    public class OnLineHandler
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="userlist"></param>
        /// <param name="timeout">秒</param>
        public static void AutoRemoveTimeOutUser(List<LoginUser> userlist, int timeout)
        {
            for (int i = userlist.Count - 1; i > -1; i--)
            {
                if ((DateTime.Now - userlist[i].LastRefreshTime).TotalSeconds > timeout)
                    userlist.RemoveAt(i);
            }
        }

        public static int GetAllOnlineUserCount(List<LoginUser> userlist, int timeout)
        {
            return userlist.FindAll(var => (DateTime.Now - var.LastRefreshTime).TotalSeconds < timeout).Count;
        }

        public static int GetGradeAllOnlineUserCount(List<LoginUser> userlist, int timeout, int collegeid, int gradeid)
        {
            return userlist.FindAll(var => (DateTime.Now - var.LastRefreshTime).TotalSeconds < timeout && var.CollegeID == collegeid && var.GradeID == gradeid).Count;
        }

        public static int GetGradeAllOnlineParentCount(List<LoginUser> userlist, int timeout, int collegeid, int gradeid)
        {
            return userlist.FindAll(var => (DateTime.Now - var.LastRefreshTime).TotalSeconds < timeout && var.CollegeID == collegeid && var.GradeID == gradeid && var.LoginUserType == UserType.Parent).Count;
        }

        public static int GetGradeAllOnlineStudentCount(List<LoginUser> userlist, int timeout, int collegeid, int gradeid)
        {
            return userlist.FindAll(var => (DateTime.Now - var.LastRefreshTime).TotalSeconds < timeout && var.CollegeID == collegeid && var.GradeID == gradeid && var.LoginUserType == UserType.Student).Count;
        }
        public static string GetGradeAllOnlineAssistant(List<LoginUser> userlist, int timeout, int collegeid, int gradeid)
        {
            List<LoginUser> asslist = userlist.FindAll(var => (DateTime.Now - var.LastRefreshTime).TotalSeconds < timeout && var.CollegeID == collegeid && var.GradeID == gradeid && var.LoginUserType == UserType.Assistant);
            string username = "";
            foreach (var item in asslist)
            {
                username += item.UserName + " ";
            }
            return username.TrimEnd(' ');
        }
    }
}